export const computedInit = function (warnState) {
  return {
    get () {
      return this.$store.state.warnValidate[warnState]
    },
    set (value) {
      this.$store.commit('warnValidate/SET_COMPUTED', { key: warnState, value: value })
    }
  }
}

export const debounce = function (func, wait, immediate) {
  let timeout, result

  let debounced = function () {
    let args = arguments

    if (timeout) clearTimeout(timeout)
    if (immediate) {
      let callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) result = func.apply(this, args)
    } else {
      timeout = setTimeout(() => {
        func.apply(this, args)
      }, wait)
    }
    return result
  }

  debounced.cancel = function () {
    clearTimeout(timeout)
    timeout = null
  }

  return debounced
}
